package dse.messages.multiversion;

import dse.*;
import dse.messages.*;

public class ShareDownloadMessageHandler extends MessageSender implements
		MessageHandler {
	private Version version;
	private VersionReader reader;
	//private int round;
	
	public ShareDownloadMessageHandler(Experiment experiment, Version version, VersionReader reader)//, int round)
	{
		super(experiment);
		this.version = version;
		this.reader = reader;
		//this.round = round;
	}
	
	@Override
	public boolean acceptMessage(Message message) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void messageInterupted(ExperimentRecorder recorder, Message message,
			double timeLeft) {
		// TODO Auto-generated method stub
		if(ExperimentRecorder.debug)
			recorder.debug(this, "msgIntrpt: " + message, timeLeft);
		reader.readVersion(recorder, message.from(), null, timeLeft);
	}

	@Override
	public void messageReceived(ExperimentRecorder recorder, Message message,
			double timeLeft) {
		// TODO Auto-generated method stub
		if(ExperimentRecorder.debug)
			recorder.debug(this, "msgRcvd: " + message, timeLeft);
		if (message.messageType() != MessageType.ShareDownload) {
			recorder.warn(
					"ShareDownloadMessageHandler recieved " + 
					message.messageType() + " message."
					);
			return;
		}
		if(reader.getTransaction().id() == -1){
			System.out.println("id/" + message.id() + " type/" + message.messageType() + " from/" + message.from() + " to/" + message.to()
					+ " start/" + message.startTime() + " arrive/" + message.endTime() + " deliver/" + message.deliverTime());
		}
		
		reader.readVersion(recorder, message.from(), version, timeLeft);
	}
}
